import {LsUtil} from '../util/LsUtil';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
import {Injectable} from '@angular/core';
import {ModelRes} from '../entity/ModelRes';
import {StringUtil} from '../util/StringUtil';
import {Constant} from '../util/Constant';
import {Title} from '@angular/platform-browser';
import {Md5} from 'ts-md5';

@Injectable()
export class GlobalService {

  public company = '家有管家';
  public isLoad = false; // http请求
  public isDialogLoad = false; // http在对话框中请求
  public dateFormat = 'yyyy-MM-dd';
  public monthFormat = 'yyyy年MM月';
  public timeFormat = 'yyyy年MM月dd日 HH:mm';
  public uploadUrl = '';


  constructor(public http: HttpClient, public msg: NzMessageService, public router: Router, public ls: LsUtil) {
  }


  public toast(msg: string) {
    this.msg.create('success', msg);
  }

  public err(msg: string) {
    this.msg.create('error', msg);
  }

  public war(msg: string) {
    this.msg.create('warning', msg);
  }

  public getUserInfo() {
    if (StringUtil.isBlank(this.ls.getObj('userInfo'))) {
      return {};
    }
    return JSON.parse(this.ls.getObj('userInfo'));
  }

  public setUserInfo(obj) {
    this.ls.setObj('userInfo', JSON.stringify(obj));
  }

  public delUserInfo() {
    this.ls.setObj('userInfo', '');
  }


  public req(url, obj = {}) {
    this.isLoad = true;
    return this.getPromise(url, obj);
  }

  public dialogReq(url, obj = {}) {
    this.isDialogLoad = true;
    return this.getPromise(url, obj);
  }


  private getPromise(url, obj = {}) {

    return new Promise((success) => {
      url = url + '?1=1';
      if (!StringUtil.isBlank(this.getUserInfo().admin)) {
        url = url + '&id=' + this.getUserInfo().admin.adminId;
      }
      this.http.post(url + '&sign=' + this.paramSign(obj), obj).subscribe((json: ModelRes) => {
        if (json.status !== 200) {
          this.err(json.info);
        } else {
          if (JSON.stringify(json.data) === '{}') {
            this.toast(json.info);
          }
          success(json);
        }

      }, res => { // 服务器无响应
        this.isLoad = false;
        this.isDialogLoad = false;
        this.err(StringUtil.NO_SERVER);
      }, () => {
        this.isLoad = false;
        this.isDialogLoad = false;
      });

    });
  }

  private paramSign(obj) {
    let paramStr = '';
    const temp = Object.keys(obj).sort();
    const sortObj = {};
    for (let i = 0; i < temp.length; i++) {
      sortObj[temp[i]] = obj[temp[i]];
    }
    for (const i in sortObj) {
      if (sortObj[i] instanceof Array) {
        sortObj[i] = JSON.stringify(sortObj[i]);
      }
      paramStr = paramStr + i + '=' + sortObj[i] + '&&';
    }
    paramStr = paramStr.slice(0, paramStr.length - 2);
    console.log(paramStr);
    return Md5.hashStr(paramStr);

  }


  toImg(img) {
    if (!StringUtil.isBlank(img)) {
      window.open(img);
    } else {
      this.war(StringUtil.NO_IMG);
    }
  }

  toHtml(html) {
    if (!StringUtil.isBlank(html)) {
      window.open(html);
    } else {
      this.war(StringUtil.NO_HTML);
    }
  }


}
